package com.day01;

import java.util.Arrays;
import java.util.Random;

/**
 * 2022年4月4日 14:21:07
 */
public class 快速排序01 {
    public static void quickSort(int arr[], int low, int high) {
        int i = low, j = high;
        int pivot = arr[low + (high - low) / 2];
        while (i <= j) {
            while (arr[i] < pivot) {
                i++;
            }
            while (arr[j] > pivot) {
                j--;
            }
            if (i <= j) {
                swap(arr, i, j);
                i++;
                j--;
            }
        }
        if (low < j) {
            quickSort(arr, low, j);
        }
        if (i < high) {
            quickSort(arr, i, high);
        }
    }

    public static void swap(int arr[], int i, int j) {
        int temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }

    public static void main(String[] args) {
        Random random = new Random();
        int arr[] = new int[100];
        int arr1[] = new int[100];
        for (int i = 0; i < arr.length; i++) {
            arr[i] = random.nextInt(1000);
        }
        System.arraycopy(arr, 0, arr1, 0, arr.length);
        Arrays.sort(arr1);
        quickSort(arr, 0, arr.length - 1);
        boolean flag = true;
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] == arr1[i]) {
                flag = flag && true;
            } else {
                flag = false;
            }
        }
        System.out.println("快速排序结果是否正确: " + (flag ? "√" : "×"));
    }
}
